//
//  JPServiceFactory.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/4/20.
//

#import "JPServiceFactory.h"
#import "JPApiDefaultService.h"

@interface JPServiceFactory ()

@property (nonatomic, strong) NSMutableDictionary *serviceStorage;

@end

@implementation JPServiceFactory

#pragma mark - getters and setters
- (NSMutableDictionary *)serviceStorage
{
    if (_serviceStorage == nil) {
        _serviceStorage = [[NSMutableDictionary alloc] init];
    }
    return _serviceStorage;
}

#pragma mark - life cycle
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    static JPServiceFactory *sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[JPServiceFactory alloc] init];
    });
    return sharedInstance;
}

#pragma mark - public methods
- (id <JPServiceProtocol>)serviceWithIdentifier:(NSString *)identifier
{
    if (self.serviceStorage[identifier] == nil) {
        self.serviceStorage[identifier] = [self newServiceWithIdentifier:identifier];
    }
    return self.serviceStorage[identifier];
}

#pragma mark - private methods
- (id <JPServiceProtocol>)newServiceWithIdentifier:(NSString *)identifier
{
    //demo service
    
    return [[JPApiDefaultService alloc] init];
    
    
}

@end
